import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1'))
channel = connection.channel()

channel.queue_declare(queue='hello')  # 创建队列，有则不操作，无则创建，避免生产者消费者启动顺序问题


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)  # 配合手动应答，告诉队列不要删除这个数据


# 监听队列（配置，还未监听，只是确认）
channel.basic_consume(queue='hello',
                      auto_ack=False,  # 手动应答
                      on_message_callback=callback)

print(" [*] Waiting for")
channel.start_consuming()  # 开始消费
